;(function($){
	$.spa_front_account_menu = $.spa_front_account_menu || {}
	$.fn.spa_front_account_menu = function(opts){
		var el = this;
		var opt = {
			height : "",
			width : "",
			allNum : 29,
			allPage : 1,
			page : 1,
			bh : "",//选中编号
			cb_bh : "",
			th : "",
			turnBtnPercent : 0.05,
			xNum : 5,
			yNum : 6,
			ct_brzd : {},
			widthOffset : 2,//调整翻页按钮水平偏移
			heightOffset : 4,//调整翻页按钮垂直偏移
			dataBack : function(){},
			closeBack : function(){},
			wait : function(){},
			goon : function(){},
		}
		var param = $.extend(opt,opts);
		param.allNum = param.xNum * param.yNum - 1
		var app = new App(el,param);
		$.spa_front_account_menu = $.extend($.spa_front_account_menu,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		var t = this;
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//翻页
			el.find("button.btn.btn-purple").unbind("click").one("click",function(){
				var direction = $(this).data("direction");
				if(direction=="up"){
					param.page = param.page-1;
				}else{
					param.page = param.page+1;
				}
				t.initDs();
			})
			
			//选菜入单
			el.find("a.btn.btn-app").unbind("click").click(function(){
				if(param.ct_brzd.no){
//					var th = $("#pageLeft").find("#th").val();
					var th = param.th;
					var json = $.parseJSON($(this).find(".json:eq(0)").html());
					el.modalFrame("destroy");
					el.modalFrame("init",function(modalEle){   
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_menu_enterAccount)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.spa_menu_enterAccount.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_menu_enterAccount({
							bmh : param.bmh,
							ct_brzd : param.ct_brzd,
							ds : json,
							wait : param.wait,
							goon : param.goon,
							dataBack : function(){
								param.dataBack();
								$("#nav-search-input").val("");
							},
							closeBack : function(){
								
							},
						})
					});
				}else{
					jf_alert("请先输入手牌")
				}
			})
		},
		initHtml : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var classJson={
				tbClass:"",
				occClass:"",
				rsvClass:"",
			}
			el.find(".pd_info").html("");
			el.find(".pd_info").empty();
			
			if(data.length > 0 && !!data[0]){
//				el.find(".pd_info").css({"width":param.width});
				param.width =  el.find(".pd_info").width();
				param.height = el.find(".pd_info").height();

				param.bhArr = [];
				var marginValue = 2//根据外间距margin取值
				$(data).each(function(key,value){
					var each_class=""//菜品的类型 普通和特殊(套餐，标准宴，多单位，特别菜)
					var showtc=""
					var istcclass=""//套餐标识
					
					//将所有suit_bj=2的转为3
					if(value.suit_bj==2){
						value.suit_bj=3
					}
						
					if(value.suit_bj!=1&&value.suit_bj!=3){//非套餐
						showtc=" hide"
					}else{
						each_class=" special"
					}
					if(value.suit_bj==1||value.suit_bj==3){
						istcclass=" tc"
					}
					var unit=""
					var dw_c = "";
					if(value.is_manyunit>0){//含有多种规格
						unit=" unit"
						each_class=" special"
						dw_c = 	value.dw_c;
					}
					var tabindex = (Math.floor(key/param.xNum)+1+(param.page-1)*param.yNum)*100 + key%param.xNum//data-original-title="'+value.mc+'"
					var html = $('<a tabindex="'+tabindex+'"  data-bh="'+value.bh+'" href="javascript:;" class="btn btn-app btn-light tooltip-info no-radius'+unit+''+each_class+''+istcclass+'" title="">'+
									'<div class="json hide">'+JSON.stringify(value)+'</div>'+
		    						'<div class="name" style="font-size:16px;">'+value.mc+'</div>'+
		    						'<div class="price" style="font-size:16px;">'+
			    						'￥'+value.dj+''+
		    						'</div>'+
		    						'<div style="font-size:12px;">'+dw_c+'</div>'+
			    					'<span class="badge badge-warning badge-left hide">0</span>'+
			    					'<div class="badge badge-pink tcNum'+showtc+'">'+value.is_tc+'</div>'+
								'</a>');
					if(value.mc.length > Math.floor(((param.width / param.xNum) - 2*marginValue) / 18)){
	    				html.find(".name").css("font-size",((param.width / param.xNum) - 2*marginValue) / value.mc.length);
	    			}
	    			el.find(".pd_info").append(html);
	    			param.bhArr[key] = value.bh;
	    		});
				var each_dish_width = (param.width)/param.xNum //TODO 
				var each_height=param.height/ param.yNum
				el.find(".pd_info").find(".btn.btn-app").css("width",(each_dish_width-2*marginValue)).css("height",(each_height) - 2*marginValue);
				if(each_height<97){
					el.find(".pd_info").find(".price").css({"font-size":"14px"})
				}else if(each_height<82){
					el.find(".pd_info").find(".price").css({"font-size":"12px"})
				}
				t.addPageTurning();
				t.bindEvent();
			}
		},
		addPageTurning : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var marginValue = 2//根据外间距margin取值
			var each_dish_width = (param.width)/param.xNum //TODO 
			var each_height=param.height/ param.yNum
			el.find(".pd_info").find(".btn.btn-app").css("width",(each_dish_width-2*marginValue)).css("height",(each_height) - 2*marginValue);
			var left = each_dish_width * (param.xNum-1) + marginValue + param.widthOffset + "px"
			var top = (each_height * (param.yNum-1))+ (param.top||el.position().top) + param.heightOffset + "px"
			var updisabled = "";
			var downdisabled = "";
			if(param.page==1){
				updisabled = "disabled";
			}
			
			if(param.page==param.allPage){
				downdisabled = "disabled";
			}
			
			var html = 	'<div style="position:absolute;width:'+(each_dish_width-2*marginValue)+'px;height:'+(each_height - 2*marginValue)+'px;left:'+left+';top:'+top+';overflow:hidden"><div class="btn-group">'+
							'<button '+updisabled+' class="btn btn-purple btn-sm" data-direction="up" style="height:'+(each_height)+'px;width:'+(each_dish_width/2 - 2*marginValue)+'px;">'+
								'<i class="ace-icon fa fa-angle-up black bigger-300"></i>'+
							'</button>'+
						'</div>'+
						'<div class="btn-group">'+
							'<button '+downdisabled+' class="btn btn-purple btn-sm" data-direction="down" style="height:'+(each_height)+'px;width:'+(each_dish_width/2 - 2*marginValue)+'px;">'+
								'<i class="ace-icon fa fa-angle-down black bigger-300"></i>'+
							'</button>'+
						'</div></div>';
			el.find(".pd_info").append(html);
		},
		initDs : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var ds = $.parseJSON(localStorage.getItem("menu"));
//			var ds = param.ds;
			var js = [];
			if(param.pym){    			
				var filter = "select * from json where (pym.indexOf('"+param.pym+"')>-1||bh.indexOf('"+param.pym+"')>-1)";
        		js = jsonsql.query(filter,ds);
    		}else if(param.cb_bh){
    			var filter = "select * from json where (cb_bh=='"+param.cb_bh+"')";
        		js = jsonsql.query( filter,ds);
    		}
			
    		var data = [];
    		$(js).each(function(k,v){
    			if(k>=(param.page-1)*param.allNum&&k<param.page*param.allNum){
    				data.push(v);
    			}
    		})
    		param.allPage = Math.ceil(js.length/param.allNum)
    		t.initHtml(data);
		},
		isFloot : function (str){
			if(!isNaN(str)){
				str = str + ".00";
			}
			return str;
		},
	}
	
	/*
	 * 私有函数
	 * 获取站点路径
	 */
	function getContextPath(){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		return path;
	}
})(jQuery);